package com.sky.sd.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysql.cj.util.StringUtils;
import com.sky.sd.common.web.response.Result;
import com.sky.sd.system.bean.dto.DicNode;
import com.sky.sd.system.bean.po.Dic;
import com.sky.sd.system.bean.po.User;
import com.sky.sd.system.bean.vo.*;
import com.sky.sd.system.service.IDicService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author 弓振
 * @since 2020-08-03
 */
@RestController
@RequestMapping("/system/dic")
public class DicController {

    @Autowired
    private IDicService dicService;

    @GetMapping("/p/list")
    @ApiOperation(value = "查询数据字典项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "name", value = "字典名", dataType = "string", paramType = "query", example = ""),
            @ApiImplicitParam(name = "code", value = "字典编码", dataType = "string", paramType = "query", example = ""),
    })
    public Result listP(DicSearchVo vo) {
        IPage<DicNode> page = dicService.listPDicPage(vo);
        return Result.success(page);
    }

    @GetMapping("/check/code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "字典编码", dataType = "string", paramType = "query", example = "",required = true)
    })
    @ApiOperation(value = "字典编码是否存在")
    public Result check(DicCheckVo vo) {
        int cnt = dicService.count(Wrappers.<Dic>lambdaQuery()
                .ne(StringUtils.isNullOrEmpty(vo.getId()), Dic::getId, vo.getId()).eq(Dic::getCode, vo.getCode()));
        return Result.success(cnt);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增数据字典项")
    public Result add(@RequestBody DicAddVo vo) {
        dicService.add(vo);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改数据字典项")
    public Result update(@RequestBody DicUpdateVo vo) {
        dicService.update(vo);
        return Result.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除数据字典项")
    public Result deleteUser(@RequestBody DeleteVo vo) {
        dicService.delete(vo);
        return Result.success();
    }
}

